package com.example.light.web.system.service;

import com.example.light.common.util.MD5Util;
import com.example.light.web.system.dao.*;
import com.example.light.web.system.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class CuserService {

    @Autowired
    private CuserRoleMapper cuserRoleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private  UserRoleMapper userRoleMapper;

    private String choseName = null;//保存前端选择的角色信息
    private String selectName=null;//搜索名字

    //查询所有用户的角色部门信息
    public String QueryAll(Model model, String rolename) {
        //角色分类时操作
        if (rolename != null || choseName != null) {
            if (rolename == null) {
                List<CuserRole> userRoles = cuserRoleMapper.ChoseRole(choseName);
                model.addAttribute("userRoles", userRoles);
                model.addAttribute("chselectName", choseName);
                selectlist(model);
            }
            choseName = rolename;
            String path = "system/roleUser";
            return path;
        }
        //查询时操作
        if (rolename != null || selectName != null) {
            if (rolename == null) {
                List<CuserRole> userRoles = cuserRoleMapper.selectByname(selectName);
                if(userRoles.size()==0){//判断结果不存在时
                    selectName = rolename;
                    List<CuserRole> userRolesOld = cuserRoleMapper.QueryAll();
                    model.addAttribute("userRoles", userRolesOld);
                    selectlist(model);
                    String path = "system/roleUser";
                    return path;
                }
                model.addAttribute("userRoles", userRoles);
                selectlist(model);
            }
            selectName = rolename;
            String path = "system/roleUser";
            return path;
        }
        List<CuserRole> userRoles = cuserRoleMapper.QueryAll();
        model.addAttribute("userRoles", userRoles);
        selectlist(model);
        String path = "system/roleUser";
        return path;
    }

    //查询select的角色名和部门名
    public void selectlist(Model model) {
        List<Role> roles = cuserRoleMapper.rolelist();
        String[] rolelist = new String[roles.size()];
        for (int i = 0; i < roles.size(); i++) {
            rolelist[i] = roles.get(i).getRoleName();
        }
        model.addAttribute("rolelist", rolelist);

        List<Department> depts = cuserRoleMapper.deptlist();
        String[] deptlist = new String[depts.size()];
        for (int i = 0; i < depts.size(); i++) {
            deptlist[i] = depts.get(i).getDeptName();
        }
        model.addAttribute("deptlist", deptlist);
    }


    //修改用户的角色部门
    public String Change(Model model, Integer[] ids, String[] roles, String[] depts) {

        List<String> rolelist = new ArrayList<String>();
        List<String> deptlist = new ArrayList<String>();
        List<Integer> idlist = new ArrayList<Integer>();
        for (int i = 0; i < roles.length; i++) {
            if (!idlist.contains(ids[i])) {
                rolelist.add(roles[i]);
                deptlist.add(depts[i]);
                idlist.add(ids[i]);
            }
        }
        List<Integer> roleIds = new ArrayList<Integer>();
        List<Integer> deptIds = new ArrayList<Integer>();

        for (String name : deptlist) {
            Department temp =departmentMapper.selectDeptId(name);
            deptIds.add(temp.getDeptId());
        }
        for (String name : rolelist) {
            Role temp =roleMapper.selectRoleId(name);
            roleIds.add(temp.getRoleId());
        }


        for(int i=0;i<idlist.size();i++){
            User user=new User();
            user.setDeptId(deptIds.get(i));
            user.setUserId(idlist.get(i));
            Date date=new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            user.setUserManipulateTime(timestamp);
            userMapper.updateByPrimaryKeySelective(user);

            UserRole userRole=new UserRole();
            userRole.setRoleId(roleIds.get(i));
            userRole.setUseId(idlist.get(i));
            userRoleMapper.updateByUserId(userRole);
        }


        String path = "system/roleUser";
        return path;
    }

    //删除用户
    public String Deleting(Integer [] ids){

        for(int id:ids){
            userRoleMapper.deleteByUserId(id);
            userMapper.deleteByPrimaryKey(id);
        }

        String path = "system/roleUser";
        return path;
    }

    //修改用户名字和密码
    public String updata(Model model,int id,String name,String password){
        User user=new User();
        user=userMapper.selectByPrimaryKey(id);
        user.setUserName(name);
        user.setUserPass(MD5Util.md5(password));
        Date date=new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        user.setUserManipulateTime(timestamp);
        userMapper.updateByPrimaryKey(user);
        return "system/roleUser";
    }

    //查询用户
    public String selectbyname(Model model,String name){
        name="%"+name+"%";
        selectName=name;
        return "system/roleUser";
    }
}
